package com.example.util.rsa;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

import com.example.util.DSAUtil;



public class CreateRSA {

	public static Map<String,String> CreateRSAKeys() throws NoSuchAlgorithmException {
		KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
		kpg.initialize(1024, new SecureRandom());
		KeyPair keys = kpg.generateKeyPair();
		PrivateKey privateKey = keys.getPrivate();
		PublicKey publicKey = keys.getPublic();
		String privateStr=DSAUtil.bytesToHex(privateKey.getEncoded());
		String publicStr=DSAUtil.bytesToHex(publicKey.getEncoded());
        Map<String,String> map=new HashMap<String,String>();
        map.put("publickey", publicStr);
        map.put("privatekey", privateStr);
        return map;
		
	}
	public static void main(String[] args) {
		try {
			 Map<String,String> map=CreateRSAKeys();
			 System.out.println("公钥："+map.get("publickey"));
			 System.out.println("私钥："+map.get("privatekey"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
